using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    [RequireComponent(typeof(CButton))]
    public class DatePickerScroll : MonoBehaviour
    {
        public Text cell;

        public int value
        {
            get
            {
                if (cells.Count == 0) return 0;
                return int.Parse(cells[currIndex].text);
            }
        }
        public Action onStop;
        private List<Text> cells = new List<Text>();
        private float space = 70;
        private int currIndex;
        private bool isDragging;
        private CButton btn;
        private Canvas canvas;
        private float prePos;

        private void Awake()
        {
            cell.gameObject.SetActive(false);
            btn = gameObject.GetComponent<CButton>();
            btn.triggerInterval = 0;
            btn.transition = CButton.Transition.None;
            btn.SetPress(() =>
            {
                isDragging = true;
                if (!canvas) canvas = GetComponentInParent<CanvasScaler>().GetComponent<Canvas>(); //不能直接获取Canvas
                prePos = ScreenHelper.ScreenToUGUIPoint(Input.mousePosition, canvas).y;
            });
            btn.SetRelease(() =>
            {
                //找要给离中心最近的索引
                int index = 0;
                float dis = float.MaxValue;
                for (int i = 0; i < cells.Count; i++)
                {
                    float _dis = Mathf.Abs(cells[i].rectTransform.anchoredPosition.y);
                    if (_dis < dis)
                    {
                        dis =  _dis;
                        index = i;
                    }
                }
                currIndex = index;
                onStop?.Invoke();
                isDragging = false;
            });
        }

        public void SetData(List<int> texts, int defaultValue)
        {
            for (int i = 0; i < cells.Count; i++)
            {
                Destroy(cells[i].gameObject);
            }
            cells.Clear();
            for (int i = 0; i < texts.Count; i++)
            {
                Text intance = Instantiate(cell, cell.transform.parent, false);
                intance.gameObject.SetActive(true);
                intance.text = texts[i].ToString();
                cells.Add(intance);
                if (defaultValue == texts[i])
                {
                    currIndex = i;
                }
            }
        }

        private void Update()
        {
            if (cells.Count == 0) return;

            Vector2 mainPos = cells[currIndex].rectTransform.anchoredPosition;
            if (isDragging)
            {
                float currPos = ScreenHelper.ScreenToUGUIPoint(Input.mousePosition, canvas).y;
                float delta = currPos - prePos;
                prePos = currPos;
                mainPos.y += delta;
            }
            else
            {
                mainPos.y += (0 - mainPos.y) * 0.5f;
            }
            cells[currIndex].rectTransform.anchoredPosition = mainPos;
            for (int i = 0; i < currIndex; i++)
            {
                Vector2 pos = cells[i].rectTransform.anchoredPosition;
                pos.y = mainPos.y + space * (currIndex - i);
                cells[i].rectTransform.anchoredPosition = pos;
            }
            for (int i = currIndex + 1; i < cells.Count; i++)
            {
                Vector2 pos = cells[i].rectTransform.anchoredPosition;
                pos.y = mainPos.y - space * (i - currIndex);
                cells[i].rectTransform.anchoredPosition = pos;
            }
        }
    }
}
